package cn.stylefeng.guns.sys.modular.metadata.controller;

import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.sys.modular.metadata.entity.Metadata;
import cn.stylefeng.guns.sys.modular.metadata.model.params.MetadataParam;
import cn.stylefeng.guns.sys.modular.metadata.service.MetadataService;
import cn.stylefeng.roses.core.base.controller.BaseController;
import cn.stylefeng.roses.kernel.model.response.ResponseData;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;


/**
 * 元数据管理表控制器
 *
 * @author author
 * @Date 2020-11-17 10:28:38
 */
@Controller
@RequestMapping("/metadata")
public class MetadataController extends BaseController {

    private String PREFIX = "/modular/metadata/metadata";

    @Autowired
    private MetadataService metadataService;

    /**
     * 跳转到主页面
     *
     * @author author
     * @Date 2020-11-17
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "/metadata.html";
    }

    /**
     * 新增页面
     *
     * @author author
     * @Date 2020-11-17
     */
    @RequestMapping("/add")
    public String add() {
        return PREFIX + "/metadata_add.html";
    }

    /**
     * 编辑页面
     *
     * @author author
     * @Date 2020-11-17
     */
    @RequestMapping("/edit")
    public String edit() {
        return PREFIX + "/metadata_edit.html";
    }

    /**
     * 查看数据页面
     *
     * @author author
     * @Date 2020-11-17
     */
    @RequestMapping("/metadataDetail")
    public String metadataDetail() {
        return PREFIX + "/metadata_detail.html";
    }

    /**
     * 新增接口
     *
     * @author author
     * @Date 2020-11-17
     */
    @RequestMapping("/addItem")
    @ResponseBody
    public ResponseData addItem(MetadataParam metadataParam) {
        this.metadataService.add(metadataParam);
        return ResponseData.success();
    }

    /**
     * 编辑接口
     *
     * @author author
     * @Date 2020-11-17
     */
    @RequestMapping("/editItem")
    @ResponseBody
    public ResponseData editItem(MetadataParam metadataParam) {
        this.metadataService.update(metadataParam);
        return ResponseData.success();
    }

    /**
     * 删除接口
     *
     * @author author
     * @Date 2020-11-17
     */
    @RequestMapping("/delete")
    @ResponseBody
    public ResponseData delete(MetadataParam metadataParam) {
        this.metadataService.delete(metadataParam);
        return ResponseData.success();
    }

    /**
     * 查看详情接口
     *
     * @author author
     * @Date 2020-11-17
     */
    @RequestMapping("/detail")
    @ResponseBody
    public ResponseData detail(MetadataParam metadataParam) {
        Metadata detail = this.metadataService.getById(metadataParam.getMetadataId());
        return ResponseData.success(detail);
    }

    /**
     * 查询列表
     *
     * @author author
     * @Date 2020-11-17
     */
    @ResponseBody
    @RequestMapping("/list")
    public LayuiPageInfo list(MetadataParam metadataParam) {
        return this.metadataService.findPageBySpec(metadataParam);
    }

    /**
     * 逻辑删除
     *
     * @param arr
     * @return
     */
    @ResponseBody
    @RequestMapping("/deleteMetadata")
    public int deleteMetadataById(@RequestParam int[] arr) {
        int number = 0;
        for (int i = 0; i < arr.length; i++) {
            metadataService.deleteMetadataInfo(arr[i]);
            number++;
        }
        return number;
    }

    /**
     * 获取元数据的详细数据
     *
     * @author author
     * @Date 2020-11-17
     */
    @ResponseBody
    @RequestMapping("/listMetadata")
    public JSONObject listMetadata(MetadataParam metadataParam, Integer page, Integer limit) {
        return this.metadataService.queryMetadataDetailInfo(metadataParam, page, limit);
    }

    @ResponseBody
    @RequestMapping("/createTable")
    public int createTable(MetadataParam metadataParam) {
        return this.metadataService.creatTable(metadataParam);
    }






    /**
     * 更新表数据量信息
     *
     * @param arr
     * @return
     */
    @ResponseBody
    @PostMapping("/metadataTotal")
    public int metadataTotal(@RequestBody MetadataParam[] arr) {
        int number = 0;
        for (int i = 0; i < arr.length; i++) {
            List<Map<String, Object>> list = this.metadataService.queryMetadataInfo(arr[i]);
            if (list.size() != arr[i].getMetadataTotal()) {
                arr[i].setMetadataTotal(list.size());
                this.metadataService.update(arr[i]);
            }
            number++;
        }
        return number;
    }

}


